let express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
//创建一个服务
let server = express();

server.listen('66', () => {
        console.log('监听端口成功!');
    })
    //请求静态资源文件


//实现跨域
server.use(cors());



server.use(function(req, res, next) {
    //删除购物车的item
    if (req.path === "/deleteCartItem") {
        res.set('Content-Type', 'text/json');
        let objstr = fs.readFileSync(path.resolve(__dirname, './client/json/cart.json'), "utf-8");
        let obj = JSON.parse(objstr);
        let index = req.query.index;
        obj = obj.filter((item, index1) => {
            return index1 != index;
        })
        fs.writeFileSync(path.resolve(__dirname, './client/json/cart.json'), JSON.stringify(obj))
        res.end();
    }
    //加号接口
    if (req.path === "/addCart") {
        res.set('Content-Type', 'text/json');
        let objstr = fs.readFileSync(path.resolve(__dirname, './client/json/cart.json'), "utf-8");
        let obj = JSON.parse(objstr);
        //获得参数
        let index = req.query.index;
        obj[index].count++;

        fs.writeFileSync(path.resolve(__dirname, './client/json/cart.json'), JSON.stringify(obj))
        res.end();
    }
    //减号接口
    if (req.path === "/minusCart") {
        res.set('Content-Type', 'text/json');
        let objstr = fs.readFileSync(path.resolve(__dirname, './client/json/cart.json'), "utf-8");
        let obj = JSON.parse(objstr);
        //获得参数
        let index = req.query.index;
        if (obj[index].count > 1) {
            obj[index].count--;
        }

        fs.writeFileSync(path.resolve(__dirname, './client/json/cart.json'), JSON.stringify(obj))
        res.end();
    }
    //查找购物车
    if (req.path === "/getCart") {
        res.set('Content-Type', 'text/json');
        let objstr = fs.readFileSync(path.resolve(__dirname, './client/json/cart.json'), "utf-8");
        //cart文件
        let obj = JSON.parse(objstr);
        let arr = [];
        let allproduct = fs.readFileSync(path.resolve(__dirname, './client/json/productdetails.json'), "utf-8");
        obj.forEach((item) => {
            let allproductObj = JSON.parse(allproduct);
            let ary = allproductObj.filter((item_inner) => {
                return item_inner.result.productId == item.id
            })
            arr.push(ary[0]);
        })
        arr.forEach((item, index) => {
            item.result.count = obj[index].count;
        })
        res.end(JSON.stringify(arr));
        return;
    }
    //添加购物车
    if (req.path === "/addToCart") {
        res.set('Content-Type', 'text/json');
        let objstr = fs.readFileSync(path.resolve(__dirname, './client/json/cart.json'), "utf-8");
        let obj = JSON.parse(objstr);
        console.log(obj, req.query);
        //对url处理
        obj.push({
            id: req.query["ID"],
            count: 1
        });
        fs.writeFileSync(path.resolve(__dirname, './client/json/cart.json'), JSON.stringify(obj))
        res.end(JSON.stringify(obj));
        return;
    }
    console.log(req.path);
    //返回allproduct
    if (req.path === '/getAllProduct') {
        res.set('Content-Type', 'text/json');
        console.log(req.query);
        let index = req.query.index;
        let obj = fs.readFileSync(path.resolve(__dirname, './client/json/allproducts.json'), 'utf-8');
        obj = JSON.parse(obj)[index];
        console.log(obj, index);
        res.end(JSON.stringify(obj));
        return;
    }
    //返回productDetail
    if (req.path === '/getProductDetail') {
        res.set('Content-Type', 'text/json');
        res.sendFile('./client/json/productdetails.json')
        return;
    }
    //返回productList
    if (req.path === '/getProductList') {
        res.set('Content-Type', 'text/json');
        res.sendFile('./client/json/productList.json')
        return;
    }
    //返回littlelist图片
    if (req.path === '/getImgList') {
        res.set('Content-Type', 'text/json');
        res.sendFile('./client/json/xiaofenleiliebiao.json')
        return;
    }
    //返回轮播图图片地址
    if (req.path === '/getBanner') {
        res.set('Content-Type', 'text/json');
        res.sendFile('./client/json/banner.json')
        return;
    }
    //设置api:getUsers返回所有用户的信息
    if (req.path === '/getUsers') {
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/users.json')
        return;
    }
    //设置api：getCustoms返回所有客户信息
    if (req.path === '/getCustoms') {
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/customs.json')
        return;
    }
    //设置api:getTasks返回所有任务的信息
    if (req.path === '/getTasks') {
        res.set('Content-Type', 'text/json');
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')
        return;
    }
    //设置api:addTasks增加任务信息
    if (req.path === '/addTasks') {
        console.log('111');
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', 'utf-8')
        taskdata = JSON.parse(taskdata);
        //新任务
        let newTask = req.query;
        let maxId = 0;
        taskdata.forEach((item => {
            if (parseFloat(item.id) > maxId) {
                maxId = parseFloat(item.id);
            }
        }))
        newTask.id = maxId + 1;
        newTask.state = "未完成";
        taskdata.push(newTask);
        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', JSON.stringify(taskdata))
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')
        console.log(taskdata, newTask);
        return;
    }
    //设置api：修改任务的状态
    if (req.path === '/changeTasks') {
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', 'utf-8');
        taskdata = JSON.parse(taskdata);
        //改变的信息id
        let changeData = req.query;
        console.log(changeData);
        taskdata.forEach((item) => {
            if (item.id == parseFloat(changeData.id) + 1) {
                item.state = '已完成';
            }
        })

        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', JSON.stringify(taskdata));

    }
    //设置api:删除任务
    if (req.path === '/deleteTasks') {
        res.set('Content-Type', 'text/json');
        let taskdata = fs.readFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', 'utf-8');
        taskdata = JSON.parse(taskdata);
        //改变的信息id
        let changeData = req.query;
        console.log(changeData);
        taskdata = taskdata.filter((item) => {
            return parseFloat(item.id) != parseFloat(changeData.id);
        })
        console.log(taskdata);
        fs.writeFileSync('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json', JSON.stringify(taskdata));
        res.sendFile('C:/Users/30313/Desktop/crm客户挂历系统后台/client/json/tasks.json')


    }
    //



})